// pages/order/order.js
import {Order} from '../order/order-model.js';
import {Cart} from '../cart/cart-model.js';
import {Address} from '../../utils/address.js';
var order = new Order();
var cart = new Cart();
var address = new Address();

Page({

  /**
   * 页面的初始数据
   */
  data: {
     id:-1 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var from = options.from;
     if(from =='cart'){
       this._fromCart(options.totalprice);
     }
  },

  /**从购物车进入到该页面 */
  _fromCart:function(totalprice){
    var that = this;
    var productsArr;
    productsArr = cart.getCartDataFromLocal(true);
    that.setData({
      productsArr: productsArr,
      account: totalprice,
      orderStatus: 0
    })
    /**获取用户地址 */
    address.getAddress((res) => {
      that._bindAddressInfo(res);
    })
  },


  /**地址编辑 */
  editAddress:function(e){
    var that = this;
    wx.chooseAddress({
      success:function(res){
        console.log('addresss:',res);
        var addressInfo = {
          name:res.userName,
          mobile:res.telNumber,
          totalDetail: address.setAddressInfo(res)
        }

        that._bindAddressInfo(addressInfo);
        address.submitAddress(res,(res)=>{
          if (!flag) {
            that.showTips('操作提示', '地址信息更新失败！');
          }
        });

      }
    })
  },


  /*绑定地址信息*/
  _bindAddressInfo: function (addressInfo) {
    this.setData({
      addressInfo: addressInfo
    });
  },


  /**支付 */
  pay:function(){
    if(!this.data.addressInfo){
      this.showTips('下单提示','请填写您的收货地址');
    }

    if(this.data.orderStatus == 0){
      this._firstTimePay();
    }else{
      this._oneMoresTimePay();
    }

  },

  /**下单 */
  _firstTimePay:function(res){
    var orderInfo = [],
      procuctInfo = this.data.productsArr,
      order = new Order();
    for (let i = 0; i < procuctInfo.length; i++) {//购买的商品
      orderInfo.push({
        product_id: procuctInfo[i].id,
        count: procuctInfo[i].counts
      });
    }

    var that = this;
    order.doOrder(orderInfo,(res)=>{
        console.log('doOrder res:',res);
        if(res.pass){//下单成功
          //更新订单状态
          var id  = res.order_id;
          that.data.id = id;
          // that.data.formCartFlag = false;
          //开始支付
          that._execPay(id);
        }else{//下单失败
          that._orderFail(res);
        }
    });


  },

  /**支付*/
  _execPay:function(id){
    var that = this;
    order.execPay(id,(statusCode)=>{
        if(statusCode !=0){//生成订单失败不清除购物车
            var flag = statusCode ==2;
            wx.navigateTo({
              url: '../pay-result/pay-result?id='+id + '&flag=' + flag + '&from = order'           
            });
        }
    });
  },



 
 /*
  * 提示窗口
  * params:
  * title - {string}标题
  * content - {string}内容
  * flag - {bool}是否跳转到 "我的页面"
  */
  showTips: function (title, content, flag) {
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
      success: function (res) {
        if (flag) {
          wx.switchTab({
            url: '/pages/my/my'
          });
        }
      }
    });
  },

  /* 获取even中的数据 */
  getEvenData: function (e, key) {
    return e.currentTarget.dataset[key];
  }


})